/**
 * 选择人员插件
 * Created by wuwt on 2015/7/20.
 * 需要用到的js：ztree，select2，icheck
 * 使用说明：在需要弹出选择人员窗口的标签加上 onclick="openLecturerModal();"
 * 确认选择人员按钮事件需要在使用的页面上实现如下方法:
     function confirmSelectLecturer(){
        var checkedLecturerIds = $("#checkedLecturerIds").val();//选择的人员id字符串集合
        if(checkedLecturerIds != ""){
            //根据id集合查询用户信息
            $.post(getLecturerListByIdsUrl,{"lecturerIds":checkedLecturerIds},function(data){
                var lecturerList = data;
                ...
            })
        }
    }
 */

var getLecturerPageUrl = "/lrm/controller/lecturer/getLecturerPage";
var getLecturerListUrl = "/lrm/controller/lecturer/getLecturerList";
var getLecturerListByIdsUrl = "/lrm/controller/lecturer/getLecturerListByIds";



$(function(){
    var div = '<div id="loadLecturerDiv"></div>';
    $("#mainScroll").after(div);
    loadLecturerModal();
})

//加载选择人员窗体组件
function loadLecturerModal(){
    $("#loadLecturerDiv").load(getLecturerPageUrl, function(){
        $("input[name='lecturerCheckboxAll']").iCheck({
            checkboxClass: "icheckbox_minimal-blue",
            radioClass: "iradio_minimal-blue",
            increaseArea: "20%"
        });
        $("input[name='lecturerCheckboxAll']").on("ifClicked", function(event){
            if(!$(this).prop("checked")){
                $("input[name='lecturerCheckboxSub']").iCheck("check");
                var checkedLecturers = $("input[name='lecturerCheckboxSub']:checked");
                var checkedLecturerArray = $("#checkedLecturerIds").val().split(",");
                var checkedLecturerStr = '';
                $.each(checkedLecturers ,function(i,checkedLecturer){
                    var lecturerId = $(checkedLecturer).val();
                    if($.inArray(lecturerId, checkedLecturerArray) < 0){
                        checkedLecturerArray.push(lecturerId);
                        var lecturerName = $(checkedLecturer).attr("lecturerName");
                        checkedLecturerStr+='<span class="btn btn-default btn-xs" id="checkedLecturer_'+lecturerId+'" style="margin: 0px 5px 5px 0px;">'+lecturerName+'<span aria-hidden="true" onclick=removeCheckedLecturer("'+lecturerId+'")>&nbsp;&times;</span></span>';
                    }
                });
                $("#checkedLecturerIds").val(array_remove("", checkedLecturerArray));
                $("#checkedLecturerArea").append(checkedLecturerStr);
            }else{
                $("input[name='lecturerCheckboxSub']").iCheck("uncheck");
                var uncheckedLecturers = $("input[name='lecturerCheckboxSub']").not("input:checked");
                $.each(uncheckedLecturers,function(i, uncheckedLecturer){
                    var lecturerId = $(uncheckedLecturer).val();
                    removeCheckedLecturer(lecturerId);
                });
            }
        });
    });
}

//打开选择讲师窗口
function openLecturerModal(){
    searchLecturerList(1);
    $("#lecturer_modal").modal("show");
}
//关闭选择讲师窗口
function closeLecturerModal(){
    $("#lecturer_modal").modal("hide");
}

//加载讲师列表
function searchLecturerList(pageNo){
    $("input[name='lecturerCheckboxAll']").iCheck("uncheck");
    var lecturerName = $("#lecturerName").val();
    var params = {"lecturerName":lecturerName, "pageNo":pageNo};
    $.post(getLecturerListUrl, params, function(data){
        var lecturerListStr = "";
        var openPage = data.openPage;
        if(openPage.rows.length > 0){
            var pageNo = openPage.pageNo;
            var pageSize = openPage.pageSize;
            var total = openPage.total;
            var totalPages = openPage.totalPages;
            var checkNum = 0;
            $.each(openPage.rows,function(i,lecturer){
                var indexNo = (pageNo-1)*pageSize+(i+1);
                var lecturerId = lecturer.lecturerId;
                var lecturerName = lecturer.lecturerName;
                var lecturerSex = lecturer.lecturerSex;
                var lecturerType = lecturer.lecturerType;
                var status = lecturer.status;
                lecturerListStr += '<tr><td><input class="lecturerCheckboxSub" type="checkbox" name="lecturerCheckboxSub" ';
                var checkedLecturerArray = $("#checkedLecturerIds").val().split(",");
                if($.inArray(lecturerId, checkedLecturerArray)>=0){
                    ++checkNum;
                    lecturerListStr += 'checked="checked" ';
                }
                lecturerListStr += 'value="'+ lecturerId +'" lecturerName="'+lecturerName+'"></td><td>'+indexNo+'</td>';
                lecturerListStr += '<td>'+lecturerName+'</td>';
                lecturerListStr += '<td>'+lecturerSex+'</td>';
                lecturerListStr += '<td>'+lecturerType+'</td>';
                lecturerListStr += '<td>'+status+'</td>';
                lecturerListStr += '</tr>';
            });
            $("#lecturerList").html(lecturerListStr);
            var checkBoxNum = $("input[name='lecturerCheckboxSub']").length;
            if(checkNum == checkBoxNum){
                $("input[name='lecturerCheckboxAll']").iCheck("check");
            }

            if(total > pageSize){
                pageLimit(pageNo,totalPages,"lecturerPageLimit");
            }

            $("input[name='lecturerCheckboxSub']").iCheck({
                checkboxClass: "icheckbox_minimal-blue",
                radioClass: "iradio_minimal-blue",
                increaseArea: "20%"
            });
            $("input[name='lecturerCheckboxSub']").on("ifClicked", function(event){
                var checkNum = $("input[name='lecturerCheckboxSub']:checked").length;
                if((checkNum+1)==checkBoxNum){
                    $("input[name='lecturerCheckboxAll']").iCheck("check");
                }
                var lecturerId = $(this).val();
                if(!$(this).prop("checked")){
                    var lecturerName = $(this).attr("lecturerName");
                    var checkedLecturerArray = $("#checkedLecturerIds").val().split(",");
                    if($.inArray(lecturerId, checkedLecturerArray) < 0){
                        checkedLecturerArray.push(lecturerId);
                        var lecturerName = $(this).attr("lecturerName");
                        var checkedLecturerStr='<span class="btn btn-default btn-xs" id="checkedLecturer_'+lecturerId+'" style="margin: 0px 5px 5px 0px;">'+lecturerName+'<span aria-hidden="true" onclick=removeCheckedLecturer("'+lecturerId+'")>&nbsp;&times;</span></span>';
                        $("#checkedLecturerIds").val(array_remove("", checkedLecturerArray));
                        $("#checkedLecturerArea").append(checkedLecturerStr);
                    }
                }else{
                    $("input[name='lecturerCheckboxAll']").iCheck("uncheck");
                    removeCheckedLecturer(lecturerId);
                }
            });
        }else{
            $("#lecturerPageBar").html("");
            lecturerListStr='<tr><td colspan="7" style="vertical-align:middle;"><h4 class="text-center"><small>没有找到相关数据</small></h4></td></tr>';
            $("#lecturerList").html(lecturerListStr);
            $("#lecturerPageLimitDiv").hide();
        }
    })
}

//数组移除某一项
function array_remove(value,array){
    var newArray = new Array();
    $.each(array,function(i,item){
        if(item != value){
            newArray.push(item);
        }
    })
    return newArray;
}

//数组去除重复值
function array_unique(array){
    array = array || [];
    var a = {};
    var len = array.length;
    for (var i=0; i<len;i++){
        var v = array[i];
        if (typeof(a[v]) == 'undefined'){
            a[v] = 1;
        }
    };
    array.length=0;
    for (var x in a){
        array[array.length] = x;
    }
    return array;
}

//移除已选的
function removeCheckedLecturer(id){
    $("#checkedLecturer_"+id).remove();
    var checkedLecturerArray = $("#checkedLecturerIds").val().split(",");
    $("#checkedLecturerIds").val(array_remove(id, checkedLecturerArray));
    $("input[name='lecturerCheckboxSub'][value='"+id+"']").iCheck("uncheck");
    $("input[name='lecturerCheckboxAll']").iCheck("uncheck");
}


//分页设置
function pageLimit(currentPage,totalPages,pageBarId) {
    debugger;
    var options = {
        currentPage: currentPage,
        totalPages: totalPages,
        numberOfPages: 5,
        bootstrapMajorVersion: 3,
        itemTexts: function (type, page, current) {
            switch (type) {
                case "first":
                    return "首页";
                case "prev":
                    return "上一页";
                case "next":
                    return "下一页";
                case "last":
                    return "尾页";
                case "page":
                    return page;
            }
        },
        tooltipTitles: function (type, page, current) {
            switch (type) {
                case "first":
                    return "首页";
                case "prev":
                    return "上一页";
                case "next":
                    return "下一页";
                case "last":
                    return "尾页";
                case "page":
                    return "第"+page+"页";
            }
        },
        onPageClicked: function (e, originalEvent, type, pageNo) {
            searchLecturerList(pageNo);
        }
    }
    $("#"+pageBarId).bootstrapPaginator(options);
    $("#"+pageBarId).addClass("pagination-sm");
}